import { createRouter, createWebHistory } from 'vue-router'
import { useUserStore } from '@/stores' // 假设用户状态存储在该路径

const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  routes: [
    {
      path: '/',
      component: () => import('@/views/layout/index.vue'),
      children: [
        {
          // 二级路径 /
          path: '',
          name: 'home',
          // 请替换为实际的组件路径
          component: () => import('@/views/home/index.vue'),
        },
        {
          // 二级路径 /board
          path: 'board',
          name: 'board',
          // 请替换为实际的组件路径
          component: () =>
            import('@/views/board/index.vue'),
        },
        {
          // 二级路径 /search
          path: 'search',
          name: 'search',
          // 请替换为实际的组件路径
          component: () =>
            import('@/views/search/index.vue'),
        },
        {
          // 二级路径 /notice
          path: 'notice',
          name: 'notice',
          // 请替换为实际的组件路径
          component: () =>
            import('@/views/notice/index.vue'),
        },
        {
          // 二级路径 /creation
          path: 'creation',
          name: 'creation',
          redirect: '/creation/manage',
          component: () =>
            import('@/views/creation/index.vue'),
          children: [
            {
              // 写文章二级路径
              path: 'create',
              name: 'createArticle',
              component: () =>
                import(
                  '@/views/creation/components/create-article/index.vue'
                ),
            },
            {
              // 文章管理二级路径
              path: 'manage',
              name: 'manageArticle',
              component: () =>
                import(
                  '@/views/creation/components/manage-article/index.vue'
                ),
            },
          ],
        },
        {
          // 二级路径 /admin
          path: 'admin',
          name: 'admin',
          redirect: '/admin/user',
          // 请替换为实际的组件路径
          component: () =>
            import('@/views/admin/index.vue'),
          children: [
            {
              // 用户管理
              path: 'user',
              name: 'admin-user',
              component: () =>
                import(
                  '@/views/admin/components/user/index.vue'
                ),
            },
            {
              // 板块管理
              path: 'board',
              name: 'admin-board',
              component: () =>
                import(
                  '@/views/admin/components/board/index.vue'
                ),
            },
          ],
        },
        {
          // 二级路径 /post
          // 二级路径 /post/:id，支持动态参数 id
          path: 'post/:id',
          name: 'post',
          // 请替换为实际的组件路径
          component: () =>
            import('@/views/post-detail/index.vue'),
        },
        {
          // 二级路径 /post-edit/:id，支持动态参数 id
          path: 'post-edit/:id',
          name: 'post-edit',
          // 请替换为实际的组件路径
          // 使用函数模式的 props，将自定义属性传递给组件
          component: () =>
            import('@/views/post-edit/index.vue'),
        },
      ],
    },
  ],
})

// 定义需要登录才能访问的路由名称
const requireAuthRoutes = [
  'creation',
  'createArticle',
  'manageArticle',
  'post-edit',
]

// 定义需要 admin 角色才能访问的路由名称
const requireAdminRoutes = [
  'admin',
  'admin-user',
  'admin-board',
]

// 添加全局前置守卫
router.beforeEach((to, from, next) => {
  const userStore = useUserStore()
  const isLoggedIn = userStore.isLogin // 假设 isLogin 是判断用户是否登录的状态
  const userRole = userStore.user.role // 假设 user.role 存储用户角色

  // 检查是否需要登录
  if (
    requireAuthRoutes.includes(to.name as string) &&
    !isLoggedIn
  ) {
    next({ name: 'home' }) // 未登录则跳转首页
    return
  }

  // 检查是否需要 admin 角色
  if (requireAdminRoutes.includes(to.name as string)) {
    if (!isLoggedIn || userRole !== 'admin') {
      next({ name: 'home' }) // 未登录或非 admin 角色则跳转首页
      return
    }
  }

  next() // 允许访问
})

export default router
